﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CCS CATWithDepartment
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-3-15
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASB.CC.Business.CCS;
using ASB.CC.Business.CIS;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace ASB.CC.Web.SystemManagement
{
    public partial class CATWithDepartment : EntityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected IEnumerable GetModules()
        {
            return Enum.GetNames(typeof(DepartmentModule));
        }

        protected IEnumerable GetModules(object data)
        {
            return Enum.GetNames(typeof(DepartmentModule)).Select(p => new
            {
                Letter = p,
                Data = data,
            });
        }

        protected IEnumerable GetDepartmentRelation(object dataItem, object letter)
        {
            string lett = letter as string;
            char[] ler = lett.ToCharArray();
          
                using (var dc = new CCEntities())
                {


                    var raw = from t in dc.Departments
                              where (t.Disabled == false||!t.Disabled.HasValue) && ((t.DepartmentNameEN).Substring(0, 1) == lett.Substring(0, 1) || (t.DepartmentNameEN).Substring(0, 1) == lett.Substring(1, 1))
                              select t;

                    if (dataItem == null)
                    {
                        return raw.ToArray();
                    }
                    else
                    {
                        var departments = dataItem as IEnumerable<ASBCC.Database.EntityFramework.Department>;
                        var ids = departments.Select(p => p.DepartmentID);
                        var source = from t in raw.ToArray()
                                     select new
                                     {
                                         t.DepartmentID,
                                         t.DepartmentNameEN,
                                         t.DepartmentNameCN,
                                         Checked = ids.Contains(t.DepartmentID) ? "checked" : "",
                                     };
                        return source.ToArray();
                    }
                }
            
        }

        protected override EntityDataSource EntitySource
        {
            get { return this.edsCategory; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntitySearchBtn
        {
            get { return this.Searchbtn; }
        }

        protected override Button EntityClearBtn
        {
            get { return this.Cleanbtn; }
        }

        protected override GridView EntityGrid
        {
            get { return this.gvCategory; }
        }

        protected override DetailsView EntityDetails
        {
            get { return this.dvCategory; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {

        }

        protected override string GetEntityWhereExpression(string keyword)
        {
            return "(it.Name like N'%" + keyword + "%'"
                 + " or it.Description like N'%" + keyword + "%')";

        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var category = e.Entity as Category;
            category.Departments.Load();
            category.Departments.Clear();

            var departmentList = this.Request.Form["departmentcheck"];
            PCATInDepHelper.InternalUpdateCAT(dc, category, departmentList);
        }



        protected void edsCategory_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var category = e.Entity as Category;
            var departments = LogInfoHelper.GetDepartmentWithCAT(category.CateID);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CID, "Category updated. id=" + category.CateID + "; name=" + category.Name + "; description=" + category.Description + "; department=" + departments);

        }
    }
}
